﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Practices.Prism.ViewModel;
using PetroMentor.Service;
using System.Collections.Generic;
using PetroMentor.Service.Common;
using System.IO;
using System.Text;
using System.Xml.Serialization;
using DIST.LR.ZHJG.Events;
using HSE.Events;
using Telerik.Windows.Controls;
using System.Windows.Data;
using HSE.Common;
using Microsoft.Practices.Prism.Commands;
using PetroMentor.Service.Entities;

namespace HSE
{
    public class AqxsViewModel: NotificationObject
    {
        public List<GridViewColumn> Columns
        {
            get;
            set;
        }
        public delegate void AttachmentButtonClickHandler(T_WHYL_AQXS current);
        public event AttachmentButtonClickHandler AttachmentButtonClickEvent;

        public ICommand attachmentCommand;
        
        
        public AqxsViewModel()
        {
            attachmentCommand =  new DelegateCommand<object>(myCommand_Execute);
            this.Columns = new List<GridViewColumn>();
            this.Columns.Add(new GridViewDataColumn() {  Width= new GridViewLength(150), Header="宣誓单位", DataMemberBinding = new Binding("DEPTNAME") });
            this.Columns.Add(new GridViewDataColumn() { Width = new GridViewLength(150), Header = "宣誓类型", DataMemberBinding = new Binding("XSLXNAME") });

            this.Columns.Add(new GridViewDataColumn() { Width = new GridViewLength(150), Header = "宣誓时间", DataMemberBinding = new Binding("XSSJ") });
            ButtonColumn btnColumn = new ButtonColumn() { Header = "现场情况" };
            btnColumn.bInfo = new ButtonInfo() { Icon = "Images/Camera.png", Alias = "", ClickDelegate = attachmentCommand, Style = "ImageButtonStyle" };
            this.Columns.Add(btnColumn);

        }
        
        public void  Select(int pageNumber, int pageSize, string whereClause)
        {
            EventAggregatorRepository.EventAggregator
               .GetEvent<BusyIndicatorEvent>().Publish(new BusyInfo() { IsBusy = true, BusyContent = "数据获取中..." });
            Type type = typeof(T_WHYL_AQXS);
            Uri endpoint = new Uri(String.Format("{0}QueryHanlder.ashx?{1}={2}&{3}={4}&{5}={6}&{7}={8}", SystemVar.ServiceUrl,
                    Consts.MODULE, type.Name, Consts.PAGE_NUMBER, pageNumber, Consts.PAGE_SIZE, pageSize, Consts.WHERE, whereClause));
            WebClient client = new WebClient();

            //在异步资源下载操作完成时发生
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
            client.DownloadStringAsync(endpoint);
        }

        private void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            try
            {
                if (e.Result != null)
                {
                    CommonQueryInfo<T_WHYL_AQXS> info = Util.JsonToObject<CommonQueryInfo<T_WHYL_AQXS>>(e.Result) as CommonQueryInfo<T_WHYL_AQXS>;
                    if (info != null)
                    {
                        EventAggregatorRepository.EventAggregator.GetEvent <QueryEvent<T_WHYL_AQXS>>().Publish(info);
                        EventAggregatorRepository.EventAggregator.GetEvent<BusyIndicatorEvent>().Publish(new BusyInfo() { IsBusy = false, BusyContent = "数据获取中..." });
                    }
                }


            }

            catch (Exception ex)
            {
                MessageBox.Show(string.Format("获取文化引领-安全宣誓发生异常！{0}",ex.Message));
                EventAggregatorRepository.EventAggregator.GetEvent<BusyIndicatorEvent>().Publish(new BusyInfo() { IsBusy = false, BusyContent = "数据获取中..." });

            }
        }

        public void myCommand_Execute(object obj)
        {
            if (obj is T_WHYL_AQXS)
            {
                this.AttachmentButtonClickEvent(obj as T_WHYL_AQXS);
            }
        }

        
        
    }
}
